<template>
    <div></div>
</template>
<script>
    import toolPackage from '../resource/js/common/toolPackage'
    import wx from 'weixin-js-sdk' //微信分享js
    
    export default {
        data () {
            return {
                config: {
                    appId: '',
                    nonceStr:'',
                    signature:'',
                    timestamp:'',
                    url: ''
                }
            }
        },
        events : {
            'init-share' : function(){
                var _self = this;
                _self.getWxsdk(_self.wxshare);
                // var _hash = window.location.hash;
                // if(_hash.indexOf('repeatshare') == -1){
                //     if(_hash.indexOf('?') == -1){
                //         window.location.hash=_hash+'?repeatshare=1'
                //     }else{
                //         window.location.hash=_hash+'&repeatshare=1'
                //     }
                //     _self.getWxsdk(_self.wxshare);
                // }else{
                //     _self.wxshare();
                // }
            }
        },
        methods: {
            //微信分享
            getWxsdk (callback){
                var _self = this;
                //微信JSSDK初始化接口
                $.ajax({
                    type: 'post',
                    url: _wap_config.wxshare,
                    data:{
                        url : window.location.href.split('#')[0]
                    },
                    success: function(res) {
                        if(res.success){

                            _self.config = res.value;

                            wx.config({
                                debug: _wap_config.wxsharedebug,
                                appId: _self.config.appId,
                                timestamp: _self.config.timestamp,
                                nonceStr: _self.config.nonceStr,
                                signature: _self.config.signature,
                                jsApiList: [
                                'onMenuShareTimeline',
                                'onMenuShareAppMessage',
                                'onMenuShareQQ',
                                'onMenuShareQZone',
                                'onMenuShareWeibo'
                                ]
                            });
                            callback && callback();
                        }
                    },
                    error: function() {
                        alert("网络未连接!");
                    }
                });
            },
            wxshare(){
                var _self = this;
                wx.ready(function () {  
                    if(_self.config.appId){
                        var shareData = {
                            title: "你的好友送你5元，请查收",
                            desc: '亲爱哒，我发现了一个轻松赚钱的途径，第一个就想到了你，快来领哟！',
                            link: _wap_config.linkUrl+window.location.hash,
                            imgUrl: _wap_config.linkUrl+'/static/img/share_logo.jpg'
                        };
                        wx.onMenuShareAppMessage(shareData);
                        wx.onMenuShareTimeline(shareData);
                        wx.onMenuShareQQ(shareData);
                        wx.onMenuShareQZone(shareData);
                        wx.onMenuShareWeibo(shareData);
                    }        
                    
                });  
            }
        }
    }
</script>
